const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
// const cssnext = require('postcss-cssnext');

var bannerPlugin = new webpack.BannerPlugin({
      banner: '// { "framework": "Vue" }\n',
      raw: true
});

//压缩代码
var UglyPlugin = new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
        drop_console: false
      },
      comments: false,
      beautify: false,
      sourceMap: false,
      mangle: {
        except: ['vue', 'webpack', 'exports', 'require']
      }
});

/**入口文件*/
let entry = require('./build/getEntry');

/**获取基本的配置*/
function getBaseConfig () {
  return {
    entry: entry,
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: '[name].js'
    },
    resolve: {
      extensions: ['.js', '.vue', '.json', '.css'],
      modules: [
        "node_modules", path.resolve(__dirname, 'src')
      ],
      alias: {
        'src': path.resolve(__dirname, './src/'),
        'assets': path.resolve(__dirname, './src/assets/'),
        'components': path.resolve(__dirname, './src/components/'),
        'constants': path.resolve(__dirname, './src/constants/'),
        'api': path.resolve(__dirname, './src/api/'),
        'modules': path.resolve(__dirname, './src/modules/'),
        'config': path.resolve(__dirname, './build/config'),
        'utils': path.resolve(__dirname, './src/utils/'),
        'filters': path.resolve(__dirname, './src/filters/'),
        'services': path.resolve(__dirname, './src/services/')
      }
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          loader: 'babel-loader?cacheDirectory',
          include: path.resolve(__dirname, './src'),
          exclude: /node_modules/
        },
        {
          test: /\.vue(\?[^?]+)?$/,
          loader: ['weex-loader'],
          exclude: /node_modules/,
          /*options: {
            loaders: {
              postcss: [
                require('autoprefixer')({
                  browsers: ['last 10 Chrome versions', 'last 5 Firefox versions', 'Safari >= 6', 'ie > 8']
                })
              ]
            }
          }*/
        },
        {
          test: /\.json$/,
          use: 'json-loader'
        },
        {
          test: /\.(css|scss)$/,
          loader:"style-loader!css-loader!postcss-loader!sass-loader"
        },
        {
          test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
          exclude: /(node_modules)/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 1,/*正式环境需要重新适配base64 1024*10*/
                name: 'assets/images/[name].[ext]',
                publicPath: '../'
              }
            }
            //, {/*新增图片压缩处理机制*/
              /*loader: 'image-webpack-loader',
              options: {
                optimizationLevel: 3,
                interlaced: false,
                pngquant:{
                  quality: "65-90",
                  speed: 4
                },
                mozjpeg: {
                  quality: 65
                }
              }
            }*/
          ]
        },
        {
          test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
          loader: 'url-loader',
          options: {
            limit: 1024*10,
            name: 'assets/fonts/[name].[ext]',
            publicPath: '../'
          }

        }
      ]
    },
    plugins: [UglyPlugin, bannerPlugin]/*顺序千万不能对调,weex需要根据bannerPlugin加载对应的vue.js  commonPlugin,*/
  }
}

module.exports = getBaseConfig();
